﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using Silverlight.Navigation.Favorites;

namespace Silverlight.Navigation.Controls
{
    public partial class Favorites : ChildWindow
    {
        #region Fields

        private FavoritesManager _favoritesManager;

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="Favorites"/> class.
        /// </summary>
        public Favorites()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Favorites"/> class.
        /// </summary>
        /// <param name="favoriteItems">The favorite items.</param>
        public Favorites(FavoritesManager favoritesManager)
            : this()
        {
            _favoritesManager = favoritesManager;
            this.FavoritesListBox.ItemsSource = favoritesManager.FavoriteItems;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the selected favorite item.
        /// </summary>
        /// <value>The selected favorite item.</value>
        public FavoriteItem SelectedFavoriteItem { get; set; }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Handles the Click event of the CloseButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        /// <summary>
        /// Handles the Click event of the HyperlinkButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            if (sender != null && sender is HyperlinkButton)
            {
                HyperlinkButton hyperlinkButton = (HyperlinkButton)sender;

                if (hyperlinkButton.DataContext != null && hyperlinkButton.DataContext is FavoriteItem)
                {
                    this.SelectedFavoriteItem = (FavoriteItem)hyperlinkButton.DataContext;
                    this.DialogResult = true;
                }
            }
        }

        /// <summary>
        /// Handles the Click event of the DeleteButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            if (sender != null && sender is Button)
            {
                Button button = (Button)sender;

                if (button.DataContext != null && button.DataContext is FavoriteItem)
                {
                    _favoritesManager.Remove((FavoriteItem)button.DataContext);
                }
            }
        }

        #endregion
    }
}
